'''
爬虫处理业务
'''
import requests
from bs4 import BeautifulSoup

class Scrawler():
    def __init__(self):
        #爬虫构造函数 接收url
        self.headers={
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.54'
        }
    #获取源代码

    #爬文本
    def getTargetText(self,url,selector):
        codes=requests.get(url, headers=self.headers).text
        bs = BeautifulSoup(codes,'html.parser')
        return str([item.text for item in bs.select(selector=selector)])
    #爬图片
    def getTargetImg(self,imgurl):
        imgc = requests.get(imgurl).content
        with open('b.jpg','wb') as f:
            f.write(imgc)
if __name__=='__main__':
    url = "http://www.baidu.com"
    s = Scrawler()
    imgurl = 'https://imagecloud.thepaper.cn/thepaper/image/246/44/870.jpg'
    print(s.getTargetImg(imgurl))